class Toast {
    constructor() {
        this.container = null;
        this.createContainer();
    }

    createContainer() {
        this.container = document.createElement('div');
        this.container.className = 'toast-container';
        document.body.appendChild(this.container);
    }

    show(message, type = 'info', duration = 3000) {
        const toast = document.createElement('div');
        toast.className = `toast ${type}`;
        
        // 添加图标
        const icon = document.createElement('i');
        switch(type) {
            case 'success':
                icon.className = 'fas fa-check-circle';
                break;
            case 'error':
                icon.className = 'fas fa-times-circle';
                break;
            case 'info':
                icon.className = 'fas fa-info-circle';
                break;
            case 'warning':
                icon.className = 'fas fa-exclamation-circle';
                break;
        }
        
        const messageSpan = document.createElement('span');
        messageSpan.textContent = message;
        
        toast.appendChild(icon);
        toast.appendChild(messageSpan);
        this.container.appendChild(toast);

        // 添加动画
        requestAnimationFrame(() => {
            toast.classList.add('show');
        });

        // 自动移除
        setTimeout(() => {
            toast.classList.add('hide');
            setTimeout(() => toast.remove(), 300);
            
            // 如果没有更多 toast，移除容器
            if (this.container.children.length === 0) {
                this.container.remove();
                this.container = null;
            }
        }, duration);
    }
}

// 创建全局实例
window.toast = new Toast(); 